<?php
namespace Admin\Controller;

use Admin\Controller\BaseController;
use Think\Controller;

class UserController extends BaseController
{
    public $obj;
    public $temp_obj;
    public $local_id;

    public function __construct()
    {
        parent::__construct();
        $this->obj = M('b2c_members');
        $this->temp_obj = M('b2c_temp_user');
        $this->_arr['title'] = '会员';
        $this->local_id = session('userInfo')['local_id'];
    }

    public function index()
    {
        $this->_arr['status'] = I('status') != '' ? I('status') :  'n';
        $this->_arr['title_two'] = '会员列表';
        $this->_arr['p'] = I('p', 1, 'intval');
        $this->_arr['member_lv_id'] = I('member_lv_id');
        $this->_arr['select'] = I('select');
        $this->_arr['value'] = trim(I('value'));
        $this->_arr['begintime'] = I('begintime');
        $this->_arr['endtime'] = I('endtime');
        $where = '1=1';
        if($this->_arr['begintime']){
            $begintime = strtotime($this->_arr['begintime']);
            $where .= " and m.regtime >= '".$begintime."'";
        }
        if($this->_arr['endtime']){
            $endtime = strtotime($this->_arr['endtime']) + 86399;
            $where .= " and m.regtime < '".$endtime."'";
        }
        if($this->_arr['status'] != 'n'){
            $where .= " and m.status = '".$this->_arr['status']."'";
        }
        if($this->_arr['member_lv_id']){
            $where .= ' and m.member_lv_id = '.$this->_arr['member_lv_id'];
        }
        if($this->_arr['select'] && $this->_arr['value']){
            switch($this->_arr['select']){
                case 'id' :
                    $where .= ' and m.member_id = '.$this->_arr['value'];
                    break;
                case 'name' :
                    $where .= " and m.name like '".$this->_arr['value']."%'";
                    break;
            }
        }

        $order_id = I('id') ? I('id') : 2;
        if ($order_id == 1) {
            $order = 'member_id asc';
            $this->_arr['rank'] = 2;
        } elseif ($order_id == 2) {
            $order = 'member_id desc';
            $this->_arr['rank'] = 1;
        }
        $data = $this->obj->alias('m')->field('m.*,lv.name lvname,s_u.status s_u_status')
            ->join("(SELECT * FROM sdb_b2c_store_user WHERE local_id={$this->local_id}) s_u ON m.member_id = s_u.member_id")
            ->join("sdb_b2c_member_lv lv ON m.member_lv_id = lv.member_lv_id", 'left')
            ->where($where)->order($order)->page($this->_arr['p'] . ','.$this->page_num)->select();

        $lv_data = M('b2c_member_lv')->getField('member_lv_id,name');
        $count = $this->obj->alias('m')->where($where)
            ->join("(SELECT * FROM sdb_b2c_store_user WHERE local_id={$this->local_id}) s_u ON m.member_id = s_u.member_id")
            ->count();// 查询满足要求的总记录数
        //echo M()->_sql();
        //dump($count);
        $Page = new \Think\Page($count, $this->page_num);// 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();// 分页显示输出
        //dump($show);
        $this->_arr['page'] = $show;// 赋值分页输出
        $this->_arr['list'] = $data;
        $this->_arr['lv_data'] = $lv_data;
        $this->_showDisplay();

    }


    /**
     * 修改
     */
    public function saveMember()
    {
        $this->_arr['title_two'] = '会员列表';
        $id = I('id') ? I('id') : $this->error('参数为空！');
        if (!is_numeric($id)) $this->error('无效参数！');
        $data = $this->obj->where(['member_id' => $id])->find();
        $store = M('b2c_member_lv')->getField('member_lv_id,name');
        $this->_arr['data'] = $data;
        $this->_arr['store'] = $store;
        $this->_showDisplay();
    }

    public function doSaveMember()
    {
        $id = I('id') ? I('id') : $this->error('参数为空！');
        if (!is_numeric($id)) $this->error('无效参数！');
        $member_lv_id = I('member_lv_id');
        $advance = I('advance');
        $experience = I('experience');
        $point = I('point');
        $remark = I('remark');
        $barcodeArr = array(
            'member_lv_id' => $member_lv_id,
            'advance' => $advance,
            'experience' => $experience,
            'point' => $point,
            'remark' => $remark,
        );
        $stat = $this->obj->where(['member_id' => $id])->data($barcodeArr)->save();
        $stat === false ? $this->error('配置失败', 'index') : $this->success('配置成功', 'index');
    }

    /**
     * 冻结解冻
     */
    public function freezeMember()
    {
        $id = I('id') ? I('id') : exit(json_encode(['msg' => '参数为空！', 'stat' => false]));
        if (!is_numeric($id)) exit(json_encode(['msg' => '无效参数！', 'stat' => false]));
        $status = I('status');
        $freeze = $status == 1 ? '2' : '1';
        $stat = M('b2c_store_user')->where(['member_id' => $id,'local_id' => $this->local_id])->data(['status' => $freeze])->save();
        if ($stat === false) {
            $status == 1 ? exit(json_encode(['msg' => '冻结失败', 'stat' => false])) : exit(json_encode(['msg' => '解冻失败', 'stat' => false]));
        } else {
            $status == 1 ? exit(json_encode(['msg' => '冻结成功', 'stat' => true])) : exit(json_encode(['msg' => '解冻成功', 'stat' => true]));
        }
    }

    /**
     *  游客列表
     */
    public function tempUser()
    {
        $this->_arr['title_two'] = '游客列表';
        $this->_arr['p'] = I('p', 1, 'intval');
        $this->_arr['source_id'] = I('source_id');
        $this->_arr['select'] = I('select');
        $this->_arr['value'] = trim(I('value'));
        $this->_arr['authentication'] = trim(I('authentication'));
        $this->_arr['begintime'] = I('begintime');
        $this->_arr['endtime'] = I('endtime');
        $where = " (t.source_local_id = {$this->local_id} or t.default_local_id = {$this->local_id})";
        if($this->_arr['begintime']){
            $begintime = strtotime($this->_arr['begintime']);
            $where .= " and t.regtime >= '".$begintime."'";
        }
        if($this->_arr['endtime']){
            $endtime = strtotime($this->_arr['endtime']) + 86399;
            $where .= " and t.regtime < '".$endtime."'";
        }

        if($this->_arr['source_id']){
            $where .= ' and t.source_id = '.$this->_arr['source_id'];
        }
        if($this->_arr['authentication']){
            $where .= ' and t.authentication = '.$this->_arr['authentication'];
        }
        if($this->_arr['select'] && $this->_arr['value']){
            switch($this->_arr['select']){
                case 't_u_id' :
                    $where .= ' and t.t_u_id = '.$this->_arr['value'];
                    break;
                case 'nickname' :
                    $where .= " and t.nickname like '".$this->_arr['value']."%'";
                    break;
                case 'openid' :
                    $where .= " and t.openid = '".$this->_arr['value']."'";
                    break;
                case 'source_local_id' :
                    $local_data = M('webpos_local_store')->getFieldByLocal_name($this->_arr['value'],'local_id');
                    //dump($local_data);die;
                    $where .= " and t.source_local_id = {$local_data}";
                    break;
                case 'default_local_id' :
                    $local_data = M('webpos_local_store')->getFieldByLocal_name($this->_arr['value'],'local_id');
                    $where .= " and t.default_local_id = {$local_data}";
                    break;

            }
        }

        $order_id = I('id') ? I('id') : 2;
        if ($order_id == 1) {
            $order = 't_u_id asc';
            $this->_arr['rank'] = 2;
        } elseif ($order_id == 2) {
            $order = 't_u_id desc';
            $this->_arr['rank'] = 1;
        }
        $data = $this->temp_obj->alias('t')->field('t.*,s1.local_name source_name,s2.local_name default_name')
            ->join("sdb_webpos_local_store s1 ON s1.local_id = t.source_local_id", 'left')
            ->join("sdb_webpos_local_store s2 ON s2.local_id = t.default_local_id", 'left')
            ->where($where)->order($order)->page($this->_arr['p'] . ','.$this->page_num)->group('t.t_u_id')->select();


        //来源
        $source = M('b2c_user_source')->getField('source_id,source_name');
        $count = $this->temp_obj->alias('t')->where($where)->count();// 查询满足要求的总记录数
        //echo M()->_sql();
        $Page = new \Think\Page($count, $this->page_num);// 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();// 分页显示输出
        $this->_arr['page'] = $show;// 赋值分页输出
        $this->_arr['list'] = $data;
        $this->_arr['source'] = $source;
        $this->_showDisplay();
    }

    public function __destruct()
    {
        unset($this->obj);
        unset($this->temp_obj);
    }


}